<?php
/*
 * Created on 17-Jan-2005
 *
 */
using('System.Data');

class DataModule extends TModule
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		//$this->Database->setDataSourceName($this->Application->getUserParameter('DSN'));			
		
		require("config.php");
		$this->Database->setDataSourceName($dsn);
		$this->Database->SetFetchMode("Associative");
		//echo "Checking database..<br\>";		

		$tables = $this->Database->MetaTables();

		//If this is an empty database, we create the tables 
		//(This is only tested with a sqlite database)
		if(sizeof($tables)==0)
		{
			//Create tables
			echo "No database found, trying to create one at config specified Data Source Name: $dsn!...";

			//Groupprivileges
			$this->Database->Execute(
				"CREATE TABLE groupprivileges (" .
				"id integer primary key," .
				"groupid integer not null default '0'," .
				"repositoryid integer not null default '0'," .
				"access tinyint(4) not null default '0'," .
				"path varchar(255) not null default ''" .				
				");" );
			
			//Groups
			$this->Database->Execute("
				CREATE TABLE groups (
  					id integer primary key,
	  				name varchar(32) NOT NULL default '',
  					adminid integer NOT NULL default '0'									
				);");
			
			//Repositories
			$this->Database->Execute("
				CREATE TABLE repositories (
					id integer  primary key,
					name varchar(32) NOT NULL default '',
					ownerid integer NOT NULL default '0'					
				);			
			");
			
			$this->Database->Execute("
				CREATE TABLE userprivileges (
  					id integer primary key,
  					userid integer NOT NULL default '0',
  					repositoryid integer NOT NULL default '0',
  					access tinyint(4) NOT NULL default '0',
  					path varchar(255) NOT NULL default ''  					
				); 
			");


			//Users
			$this->Database->Execute("
				CREATE TABLE users (
  					id integer primary key,
  					name varchar(32) NOT NULL default '',
  					password varchar(32) NOT NULL default '',
  					email varchar(128) NOT NULL default '',
  					admin tinyint(4) NOT NULL default '0',
  					repositorygrants tinyint(11) NOT NULL default '0'  					
				);
			");
			
			//Usersgroups
			$this->Database->Execute("
				CREATE TABLE usersgroups (
  					userid integer default '0',
  					groupid integer default '0'
				);
			");

			//Usertickets
			$this->Database->Execute("
				CREATE TABLE usertickets (
  					email varchar(128) NOT NULL default '',
  					ticket varchar(32) NOT NULL default '',
  					repositorygrants tinyint(11) NOT NULL default '0'
				);
			");
		
			echo "done!</br>";
			echo "Please reload page!";
			exit(0);			
		}
		
		//Database should contain following tables
		$check = array();
		$check[]="groupprivileges";
		$check[]="groups";
		$check[]="repositories";
		$check[]="userprivileges";
		$check[]="users";
		$check[]="usersgroups";
		$check[]="usertickets";

		if($check!=$tables)
		{
			echo "Something wrong with database!";			
			exit(-1);
		}		

	}
 
} 
?>